package com.gaussian.property.admin.module.biz.service;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaussian.property.admin.module.biz.vo.suggest.ReplyForm;
import com.gaussian.property.admin.module.biz.vo.suggest.SuggestReplyVo;
import com.gaussian.property.module.biz.entity.ProjectSuggestion;
import com.gaussian.property.module.biz.entity.ProjectSuggestionReply;
import com.gaussian.property.module.biz.entity.TagLib;
import com.gaussian.property.module.biz.mapper.ProjectSuggestionMapper;
import com.gaussian.property.module.biz.mapper.ProjectSuggestionTagMapper;
import com.gaussian.property.module.biz.service.IProjectSuggestionReplyService;
import com.gaussian.property.module.biz.service.IProjectSuggestionService;
import com.gaussian.property.module.biz.vo.suggest.PageQueryProjectSuggestRequest;
import com.gaussian.property.module.biz.vo.suggest.PageQueryProjectSuggestResponse;
import com.gaussian.property.module.system.entity.SysUser;
import com.songqi.common.core.constant.Constants;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.core.utils.BeanCopyUtil;
import com.songqi.common.mybatis.core.util.MyBatisUtils;
import com.songqi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/4/18 21:40
 */
@Service
public class SuggestService {

    @Resource
    private IProjectSuggestionService iProjectSuggestionService;

    @Resource
    private IProjectSuggestionReplyService iProjectSuggestionReplyService;

    @Resource
    private ProjectSuggestionMapper projectSuggestionMapper;
    @Resource
    private ProjectSuggestionTagMapper projectSuggestionTagMapper;


    public IPage<PageQueryProjectSuggestResponse> pageQueryProjectSuggest(PageQueryProjectSuggestRequest queryRequest) {
        IPage<PageQueryProjectSuggestResponse> iPage = MyBatisUtils.buildPage(queryRequest);
        IPage<PageQueryProjectSuggestResponse> pageData = projectSuggestionMapper.pageQueryProjectSuggest(iPage, queryRequest);
        if(CollectionUtil.isNotEmpty(pageData.getRecords())){
            for (PageQueryProjectSuggestResponse record : pageData.getRecords()) {
                record.setTagNameList(getTagNameList(record.getProjectSuggestionId()));
                record.setReplierRealNameList(getReplierRealNameList(record.getProjectSuggestionId()));
            }
        }
        return pageData;
    }

    private List<String> getReplierRealNameList(String projectSuggestionId) {
        LambdaQueryWrapper<ProjectSuggestionReply> queryWrapper = new LambdaQueryWrapper<ProjectSuggestionReply>()
                .eq(ProjectSuggestionReply::getProjectSuggestionId, projectSuggestionId)
                .orderByDesc(ProjectSuggestionReply::getCreateTime);
        List<ProjectSuggestionReply> replyList = iProjectSuggestionReplyService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(replyList)){
            return replyList.stream().distinct().map(ProjectSuggestionReply::getReplierRealName).collect(Collectors.toList());
        }
        return null;
    }

    private List<String> getTagNameList(String projectSuggestionId) {
        List<TagLib> tagList = projectSuggestionTagMapper.getTagList(projectSuggestionId);
        if(CollectionUtil.isNotEmpty(tagList)){
            return tagList.stream().distinct().map(TagLib::getTagName).collect(Collectors.toList());
        }
        return null;
    }


    public List<SuggestReplyVo> querySuggestionReply(String projectSuggestionId) {
        LambdaQueryWrapper<ProjectSuggestionReply> queryWrapper = new LambdaQueryWrapper<ProjectSuggestionReply>()
                .eq(ProjectSuggestionReply::getProjectSuggestionId, projectSuggestionId)
                .orderByDesc(ProjectSuggestionReply::getCreateTime);
        List<ProjectSuggestionReply> list = iProjectSuggestionReplyService.list(queryWrapper);
        return BeanCopyUtil.copyList(list, SuggestReplyVo.class);
    }


    @Transactional(rollbackFor = Exception.class)
    public void submitReply(ReplyForm replyForm) {
        String projectSuggestionId = replyForm.getProjectSuggestionId();
        ProjectSuggestion projectSuggestion = iProjectSuggestionService.getById(projectSuggestionId);
        if (projectSuggestion == null) {
            throw new CustomException("【参数错误】项目建议id不存在");
        }

        projectSuggestion.setIsReply(Constants.YES_FLAG);
        projectSuggestionMapper.updateById(projectSuggestion);

        SysUser sysUser = (SysUser) SecurityUtils.getTokenUser();
        ProjectSuggestionReply reply = new ProjectSuggestionReply();
        reply.setProjectSuggestionId(projectSuggestionId);
        reply.setContent(replyForm.getContent());
        reply.setReplierRealName(sysUser.getNickName());
        reply.setReplySource(Constants.ADMIN_SOURCE);
        iProjectSuggestionReplyService.save(reply);

        if(CollectionUtil.isNotEmpty(replyForm.getDeleteProjectSuggestionReplyIdList())){
            iProjectSuggestionReplyService.removeBatchByIds(replyForm.getDeleteProjectSuggestionReplyIdList());
        }

    }

    public void deleteReply(String projectSuggestionReplyId) {

        ProjectSuggestionReply suggestReply = iProjectSuggestionReplyService.getById(projectSuggestionReplyId);
        String projectSuggestionId = suggestReply.getProjectSuggestionId();
        iProjectSuggestionReplyService.removeById(suggestReply);
        //统计该建议下是否还有回复
        LambdaQueryWrapper<ProjectSuggestionReply> queryWrapper = new LambdaQueryWrapper<ProjectSuggestionReply>()
                .eq(ProjectSuggestionReply::getProjectSuggestionId, projectSuggestionId);
        long replyCount = iProjectSuggestionReplyService.count(queryWrapper);
        if(replyCount == 0){
            ProjectSuggestion projectSuggestion = iProjectSuggestionService.getById(projectSuggestionId);
            projectSuggestion.setIsReply(Constants.NO_FLAG);
            projectSuggestionMapper.updateById(projectSuggestion);
        }
    }
}
